﻿using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Opds_Viewer.ViewModel
{
    public class ParameterViewModel : ViewModelBase
    {

        #region Private fields
        
        // Commanding
        private readonly RelayCommand _saveSettingsCommand;
        private readonly RelayCommand _cancelChangesCommand;
        private readonly RelayCommand _resetSettingsCommand;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public ParameterViewModel()
        {
            // Initialization of the commands
            _saveSettingsCommand = new RelayCommand(SaveSettings);
            _cancelChangesCommand = new RelayCommand(ReloadSettings);
            _resetSettingsCommand = new RelayCommand(ResetSettings);
        }

        #endregion

        #region Commanding

        /// <summary>
        /// Save the new settings
        /// </summary>
        public ICommand SaveSettingsCommand
        {
            get { return _saveSettingsCommand; }
        }

        /// <summary>
        /// Cancel the changes made by the user
        /// </summary>
        public ICommand CancelChangesCommand
        {
            get { return _cancelChangesCommand; }
        }

        /// <summary>
        /// Reset the settings with the default values
        /// </summary>
        public ICommand ResetSettingsCommand
        {
            get { return _resetSettingsCommand; }
        }

        #endregion
        
        #region private functions

        /// <summary>
        /// Load the setting values from the config file
        /// </summary>
        private void ReloadSettings()
        {
            Properties.Settings.Default.Reload();
        }

        /// <summary>
        /// Restore default values for the settings
        /// </summary>
        private void ResetSettings()
        {
            Properties.Settings.Default.Reset();
        }

        /// <summary>
        /// Save the settings of the user in the config file
        /// </summary>
        private void SaveSettings()
        {
            Properties.Settings.Default.Save();
        }

        #endregion
    }
}
